package site;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.R;

import java.util.List;

import dateBase.Judge_Site_DBHelper;
import dateBase.SiteAdapter;
import util.ToastUtil;

public class Site_search extends AppCompatActivity {
    private Spinner spinner;
    private Judge_Site_DBHelper mHelper;
    private String selectedItem;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_site_search);

        spinnerinit();
    }

    //下拉菜单，我把监听修改了
    public void spinnerinit() {
        spinner = (Spinner) findViewById(R.id.spinnerOne);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.site_type, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                String project = parent.getItemAtPosition(position).toString();
                List<SiteBean> list = mHelper.queryByType(project);
                ListView listView = findViewById(R.id.lv_JS);
                if (list.isEmpty()) {
                    listView.setAdapter(null);
                    ToastUtil.show(Site_search.this, "该信息为空");
                } else {
                    SiteAdapter adapter = new SiteAdapter(Site_search.this, list);
                    listView.setAdapter(adapter);
                    ToastUtil.show(Site_search.this, project);
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 处理未选择任何选项的事件
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mHelper = Judge_Site_DBHelper.getInstance(this);
        //打开读写链接
        mHelper.openReadLink();
        mHelper.openWriteLink();
        //获得帮助器实例
    }

    @Override
    protected void onStop() {
        super.onStop();
        mHelper.closeLink();
    }

}

